<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>10-编程式导航</title>
</head>
<body>
  <div id="app">
    <h1>Hello VueRouter!</h1>
    <router-link to="/user/1">导航一</router-link>
    <router-link :to="{path:'/user/2'}">导航二</router-link>
    <router-link :to="{path:'/user/33',query:{id:3}}">导航三</router-link>
    <router-link :to="{path:'xxxUser',params:{id:4}}">导航四</router-link>
    <hr>
    <button @click='handleNav1'>导航一</button>
    <button @click='handleNav2'>导航二</button>
    <button @click='handleNav3'>导航三</button>
    <button @click='handleNav4'>导航四</button>
    <router-view></router-view>
  </div>
</body>
<!-- 引入vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 引入VueRouter -->
<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js"></script>
<script>
  const User ={
    template:`<div>
        <h1>这里是Uesr组件 {{$route.params.id}}</h1>
        <h2>这里是User组件 {{$route.query}}</h2>
      </div>`
  }
  var routes =[
    {path:'',component:User,name:'xxx'},
    {path:'/user/:id',component:User,name:'xxxUser'},
  ];
  var router = new VueRouter({
    routes
  });
  var app = new Vue({
    el:'#app',
    router,
    methods:{
      handleNav1(){
        this.$router.push('/user/1');
      },
      handleNav2(){
        this.$router.push({path:'/user/2'});
      },
      handleNav3(){
        this.$router.push({path:'/user/33',query:{id:3}});
      },
      handleNav4(){
        this.$router.push({name:'xxxUser',params:{id:4}});
      }
    }
  })
</script>
</html>